<!-- 游戏编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改计费点' : '添加计费点'"
    :visible="visible"
    width="1000px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="计费点:" prop="product_id">
        <el-input
          v-model="form.product_id"
          placeholder="请输入计费点"
          clearable
        />
      </el-form-item>
      <el-form-item label="计费点别名:">
        <el-input
          v-model="form.alias"
          placeholder="请输入计费点别名"
          clearable
        />
      </el-form-item>
      <el-form-item label="渠道名称:" prop="channel_id">
        <el-select v-model="form.channel_id" placeholder="请选择渠道名称">
          <el-option
            v-for="item in channel_name_list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="金额 rmb:" prop="price">
        <el-input v-model="form.price" placeholder="请输入金额" clearable />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "GamePointEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: {
      type: [Object, String],
      default: () => ({}) // 默认值为一个空对象，你也可以根据需要改为其他值
    },
    gameID: {
      type: [Number, String]
    }
  },
  components: {},
  data() {
    return {
      status: 1,
      // 表单数据
      form: Object.assign({ status: 1 }, this.data),
      where: {},
      // 表单验证规则
      rules: {
        product_id: [
          { required: true, message: "请输入计费点", trigger: "blur" }
        ],
        alias: [
          { required: true, message: "请输入计费点别名", trigger: "blur" }
        ],
        channel_id: [
          { required: true, message: "请输入渠道名称", trigger: "blur" }
        ],
        price: [{ required: true, message: "请输入金额", trigger: "blur" }]
      },
      channel_name_list: [],
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    "form.channel_id"(val) {
      if (val === 0) {
        this.$delete(this.form, "pay_notify_url");
      } else {
        this.$set(this.form, "pay_notify_url", "");
      }
    },
    visible(val) {
      if (val) {
        if (this.data) {
          this.form = Object.assign({}, this.data);
          this.isUpdate = true;
        } else {
          this.form = {};
          this.isUpdate = false;
        }
      }
    }
  },
  created() {
    this.getChannelNameList();
  },
  methods: {
    /* 保存编辑 */
    // changeAppid(val) {
    //   this.$set(this.form, 'app_id', val)
    //   console.log(this.form)
    // },

    getChannelNameList() {
      this.$http.post("/channel/index").then(res => {
        console.log(res, "res");
        this.channel_name_list = res.data.data;
        console.log(this.channel_name_list);
      });
    },
    save() {
      console.log("ggg", this.gameID);
      this.form.game_id = this.gameID;
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/Product/edit", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    }
  }
};
</script>

<style scoped></style>
